/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections;
using System.Security.Permissions;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.UI.WebControls;

using DotNetNuke.ModuleToolkit.Common;
using KDLocalization = DotNetNuke.ModuleToolkit.Localization.Localization;

#endregion

namespace DotNetNuke.ModuleToolkit.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The FileControl Class provides a File Picker Control for DotNetNuke
    /// </summary>
    /// <history>
    /// 	[cnurse]	07/31/2006
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:FileControl runat=\"server\"> </{0}:FileControl>")]
    public class FileControl : CompositeControl
    {
        
        #region Public Enums

        /// <summary>
        /// Represents a possible mode for the File Control
        /// </summary>
        protected enum FileControlMode
        {
            /// <summary>
            /// The File Control is in its Normal mode
            /// </summary>
            Normal,

            /// <summary>
            /// The File Control is in the Upload File mode
            /// </summary>
            UpLoadFile,
            
            /// <summary>
            /// The File Control is in the Preview mode
            /// </summary>
            Preview
        }

        #endregion

        #region Controls

        private HtmlTable fileTable;

        //Files
        private HtmlTableRow folderRow;
        private HtmlTableCell folderCell;
        private HtmlTableCell preViewCell;
        private Label lblFolder;
        private DropDownList cboFolders;

        private HtmlTableRow fileRow;
        private HtmlTableCell fileCell;
        private Label lblFile;
        private Image imgPreview;
        private DropDownList cboFiles;
        private HtmlInputFile txtFile;

        //Command Row
        private HtmlTableRow commandRow;
        private HtmlTableCell commandCell;
        private CommandButton cmdUpload;
        private CommandButton cmdSave;
        private CommandButton cmdCancel;

        //essages
        private HtmlTableRow messageRow;
        private HtmlTableCell messageCell;
        private Label lblMessage;

        #endregion

        #region Private Members

        private const int MaxHeight = 100;
        private const int MaxWidth = 135;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the FilePath for the control
        /// </summary>
        /// <value>A String</value>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string FilePath
        {
            get
            {
                return Convert.ToString(ViewState["FilePath"]);
            }
            set
            {
                ViewState["FilePath"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the control is on a Host or Portal Tab
        /// </summary>
        /// <value>A Boolean</value>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected bool IsHost
        {
            get
            {
                bool _IsHost = Null.NullBoolean;
                if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId)
                    _IsHost = true;
                return _IsHost;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the current mode of the control
        /// </summary>
        /// <remarks>Defaults to FileControlMode.Normal</remarks>
        /// <value>A FileControlMode enum</value>
        /// <history>
        /// 	[cnurse]	7/13/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected FileControlMode Mode
        {
            get
            {
                if (ViewState["Mode"] == null)
                    return FileControlMode.Normal;
                else
                    return (FileControlMode)ViewState["Mode"];
            }
            set
            {
                ViewState["Mode"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the root folder for the control
        /// </summary>
        /// <value>A String</value>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected string ParentFolder
        {
            get
            {
                string _ParentFolder;
                if (IsHost)
                    _ParentFolder = Globals.HostMapPath;
                else
                    _ParentFolder = PortalSettings.HomeDirectoryMapPath;
                return _ParentFolder;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the file PortalId to use
        /// </summary>
        /// <remarks>Defaults to PortalSettings.PortalId</remarks>
        /// <value>An Integer</value>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected int PortalId
        {
            get
            {
                int _PortalId = Null.NullInteger;
                if (!IsHost)
                    _PortalId = PortalSettings.PortalId;
                return _PortalId;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the current Portal Settings
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected PortalSettings PortalSettings
        {
            get
            {
                return PortalController.GetCurrentPortalSettings();
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the class to be used for the Labels
        /// </summary>
        /// <remarks>Defaults to 'CommandButton'</remarks>
        /// <value>A String</value>
        /// <history>
        /// 	[cnurse]	7/13/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string CommandCssClass
        {
            get
            {
                string _Class = Convert.ToString(ViewState["CommandCssClass"]);
                if (String.IsNullOrEmpty(_Class))
                    return "CommandButton";
                else
                    return _Class;
            }
            set
            {
                ViewState["CommandCssClass"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the file Filter to use
        /// </summary>
        /// <remarks>Defaults to ''</remarks>
        /// <value>A String</value>
        /// <history>
        /// 	[cnurse]	7/13/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string FileFilter
        {
            get
            {
                if (ViewState["FileFilter"] != null)
                    return (string)ViewState["FileFilter"];
                else
                    return "";
            }
            set
            {
                ViewState["FileFilter"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the FileID for the control
        /// </summary>
        /// <value>An Integer</value>
        /// <history>
        /// 	[cnurse]	7/13/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public int FileID
        {
            get
            {
                this.EnsureChildControls();
                if (ViewState["FileID"] == null)
                    //Get FileId from the file combo
                    ViewState["FileID"] = Int32.Parse(cboFiles.SelectedItem.Value);
                return Convert.ToInt32(ViewState["FileID"]);
            }
            set
            {
                this.EnsureChildControls();
                ViewState["FileID"] = value;
                if (String.IsNullOrEmpty(FilePath))
                {
                    FileController fileController = new FileController();
                    FileInfo fileInfo = fileController.GetFileById(value, PortalId);
                    if (fileInfo != null)
                        SetFilePath(fileInfo.Folder + fileInfo.FileName);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the class to be used for the Labels
        /// </summary>
        /// <remarks>Defaults to 'NormalBold'</remarks>
        /// <value>A String</value>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string LabelCssClass
        {
            get
            {
                string _Class = Convert.ToString(ViewState["LabelCssClass"]);
                if (string.IsNullOrEmpty(_Class))
                    return "NormalBold";
                else
                    return _Class;
            }
            set
            {
                ViewState["LabelCssClass"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets whether the combos have a "Not Specified" option
        /// </summary>
        /// <remarks>Defaults to True (ie no "Not Specified")</remarks>
        /// <value>A Boolean</value>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool Required
        {
            get
            {
                if (ViewState["Required"] == null)
                    return false;
                else
                    return Convert.ToBoolean(ViewState["Required"]);
            }
            set
            {
                ViewState["Required"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets whether to Show Database Folders
        /// </summary>
        /// <remarks>Defaults to false</remarks>
        /// <value>A Boolean</value>
        /// <history>
        /// 	[cnurse]	7/31/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool ShowDatabase
        {
            get
            {
                if (ViewState["ShowDatabase"] == null)
                    return false;
                else
                    return Convert.ToBoolean(ViewState["ShowDatabase"]);
            }
            set
            {
                ViewState["ShowDatabase"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets whether to Show Secure Folders
        /// </summary>
        /// <remarks>Defaults to false</remarks>
        /// <value>A Boolean</value>
        /// <history>
        /// 	[cnurse]	7/31/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool ShowSecure
        {
            get
            {
                if (ViewState["ShowSecure"] == null)
                    return false;
                else
                    return Convert.ToBoolean(ViewState["ShowSecure"]);
            }
            set
            {
                ViewState["ShowSecure"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets whether to Show the Upload Button
        /// </summary>
        /// <remarks>Defaults to True</remarks>
        /// <value>A Boolean</value>
        /// <history>
        /// 	[cnurse]	7/31/2005  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool ShowUpLoad
        {
            get
            {
                if (ViewState["ShowUpLoad"] == null)
                    return true;
                else
                    return Convert.ToBoolean(ViewState["ShowUpLoad"]);
            }
            set
            {
                ViewState["ShowUpLoad"] = value;
            }
        }

        #endregion

        #region Private Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddButton adds a button to the Command Row
        /// </summary>
        /// <param name="button">The button to add to the Row</param>
        /// <param name="imageUrl">The url to the image for the button</param>
        /// <history>
        /// 	[cnurse]	07/31/2006 created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddButton(ref CommandButton button, string imageUrl)
        {
            button = new CommandButton();
            button.EnableViewState = false;
            button.CausesValidation = false;
            button.ControlStyle.CssClass = CommandCssClass;
            if (!String.IsNullOrEmpty(imageUrl))
            {
                button.DisplayIcon = true;
                button.ImageUrl = imageUrl;
            }
            button.Visible = false;
            commandCell.Controls.Add(button);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddCommandRow adds the Command Row
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddCommandRow()
        {
            //Create Command Row
            commandRow = new HtmlTableRow();
            commandRow.Visible = false;
            commandCell = new HtmlTableCell();
            AddButton(ref cmdUpload, "~/images/up.gif");
            cmdUpload.Click += new EventHandler(UploadFile);
            AddButton(ref cmdSave, "~/images/save.gif");
            cmdSave.Click += new EventHandler(SaveFile);

            //Add separator
            commandCell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

            AddButton(ref cmdCancel, "~/images/lt.gif");
            cmdCancel.Click += new EventHandler(CancelUpload);

            commandRow.Cells.Add(commandCell);
            fileTable.Rows.Add(commandRow);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddFileRow adds the Files Row
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddFileRow()
        {
            //Create Url Row
            fileRow = new HtmlTableRow();
            fileCell = new HtmlTableCell();

            //Create File Label
            lblFile = new Label();
            lblFile.EnableViewState = false;
            fileCell.Controls.Add(lblFile);

            //Add <br>
            fileCell.Controls.Add(new LiteralControl("<br/>"));

            //Create Files Combo
            cboFiles = new DropDownList();
            cboFiles.DataTextField = "Text";
            cboFiles.DataValueField = "Value";
            cboFiles.AutoPostBack = true;
            cboFiles.SelectedIndexChanged += new EventHandler(FileChanged);
            fileCell.Controls.Add(cboFiles);

            //Create Upload Box
            txtFile = new HtmlInputFile();
            fileCell.Controls.Add(txtFile);

            //Add Cell/row/table
            fileRow.Cells.Add(fileCell);
            fileTable.Rows.Add(fileRow);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddFolderRow adds the Folders Row
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddFolderRow()
        {
            //Create Url Row
            folderRow = new HtmlTableRow();
            folderCell = new HtmlTableCell();

            //Create Folder Label
            lblFolder = new Label();
            lblFolder.EnableViewState = false;
            folderCell.Controls.Add(lblFolder);

            //Add <br>
            folderCell.Controls.Add(new LiteralControl("<br/>"));

            //Create Folders Combo
            cboFolders = new DropDownList();
            cboFolders.AutoPostBack = true;
            cboFolders.SelectedIndexChanged += new EventHandler(FolderChanged);
            folderCell.Controls.Add(cboFolders);

            //Load Folders
            LoadFolders();

            //Add Preview
            preViewCell = new HtmlTableCell();
            preViewCell.VAlign = "top";
            preViewCell.RowSpan = 3;

            imgPreview = new Image();
            preViewCell.Controls.Add(imgPreview);

            //Add Cell/row/table
            folderRow.Cells.Add(folderCell);
            folderRow.Cells.Add(preViewCell);
            fileTable.Rows.Add(folderRow);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddMessageRow adds the Message Row
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddMessageRow()
        {
            //Create Type Row
            messageRow = new HtmlTableRow();
            messageCell = new HtmlTableCell();
            messageCell.ColSpan = 2;

            //Create Label
            lblMessage = new Label();
            lblMessage.EnableViewState = false;
            lblMessage.CssClass = "NormalRed";
            lblMessage.Text = "";
            messageCell.Controls.Add(lblMessage);

            //Add to Table
            messageRow.Cells.Add(messageCell);
            fileTable.Rows.Add(messageRow);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetFileList fetches the list of files to display in the File combo
        /// </summary>
        /// <param name="NoneSpecified">A flag indicating whether the NoneSpecified item is 
        /// shown in the list</param>
        /// <param name="Folder">The folder to fetch the list of files</param>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private ArrayList GetFileList(bool NoneSpecified, string Folder)
        {
            ArrayList fileList;

            if (IsHost)
                fileList = Globals.GetFileList(Null.NullInteger, FileFilter, NoneSpecified, cboFolders.SelectedItem.Value);
            else
                fileList = Globals.GetFileList(PortalId, FileFilter, NoneSpecified, cboFolders.SelectedItem.Value);

            return fileList;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetReadRoles fetches the list of roles that can read the folder list
        /// </summary>
        /// <param name="folder">The folder to fetch the read roles</param>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private string GetReadRoles(string folder)
        {
            return FileSystemUtils.GetRoles(folder, PortalId, "READ");
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetWriteRoles fetches the list of roles that can write the folder list
        /// </summary>
        /// <param name="folder">The folder to fetch the write roles</param>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private string GetWriteRoles(string folder)
        {
            return FileSystemUtils.GetRoles(folder, PortalId, "WRITE");
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadFolders fetches the list of folders from the Database
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void LoadFolders()
        {
            string readRoles = Null.NullString;
            string writeRoles = Null.NullString;

            cboFolders.Items.Clear();

            ArrayList folders = FileSystemUtils.GetFolders(PortalId);
            foreach(FolderInfo folder in folders)
            {
                ListItem folderItem = new ListItem();
                if (folder.FolderPath == Null.NullString)
                {
                    folderItem.Text = KDLocalization.LocalizeString(this, "Root");
                    readRoles = GetReadRoles("");
                    writeRoles = GetWriteRoles("");
                }
                else
                {
                    folderItem.Text = folder.FolderPath;
                    readRoles = GetReadRoles(folderItem.Text);
                    writeRoles = GetWriteRoles(folderItem.Text);
                }
                folderItem.Value = folder.FolderPath;

                if (PortalSecurity.IsInRoles(readRoles) || PortalSecurity.IsInRoles(writeRoles))
                {
                    bool canAdd = true;
                    switch (folder.StorageLocation)
                    {
                        case (int)FolderController.StorageLocationTypes.DatabaseSecure:
                            canAdd = ShowDatabase;
                            break;
                        case (int)FolderController.StorageLocationTypes.SecureFileSystem:
                            canAdd = ShowSecure;
                            break;
                    }
                    if (canAdd)
                        cboFolders.Items.Add(folderItem);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// SetFilePath sets the FilePath property
        /// </summary>
        /// <remarks>This overload uses the selected item in the Folder combo</remarks>
        /// <history>
        /// 	[cnurse]	08/01/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void SetFilePath()
        {
            SetFilePath(cboFiles.SelectedItem.Text);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// SetFilePath sets the FilePath property
        /// </summary>
        /// <remarks>This overload allows the caller to specify a file</remarks>
        /// <param name="fileName">The filename to use in setting the property</param>
        /// <history>
        /// 	[cnurse]	08/01/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void SetFilePath(string fileName)
        {
            if (String.IsNullOrEmpty(cboFolders.SelectedItem.Value))
                FilePath = fileName;
            else
                FilePath = cboFolders.SelectedItem.Value + "/" + fileName;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ShowButton configures and displays a button
        /// </summary>
        /// <param name="button">The button to configure</param>
        /// <param name="command">The command name (amd key) of the button</param>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void ShowButton(CommandButton button, string command)
        {
            button.Visible = true;
            if (!string.IsNullOrEmpty(command))
                button.ResourceKey = String.Format("{0}.Text", command);
            commandRow.Visible = true;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ShowImage displays the Preview Image
        /// </summary>
        /// <history>
        /// 	[cnurse]	08/01/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void ShowImage()
        {
            FileController objController = new FileController();
            FileInfo image = objController.GetFileById(FileID, PortalId);

            if (image != null)
            {
                imgPreview.ImageUrl = Globals.LinkClick("fileid=" + FileID.ToString(), PortalSettings.ActiveTab.TabID, Null.NullInteger);

                ImageUtil.CreateThumbnail(image, imgPreview, MaxWidth, MaxHeight);

                imgPreview.Visible = true;
            }
            else
                imgPreview.Visible = false;
        }

        #endregion

        #region Protected Methods

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateChildControls overrides the Base class's method to correctly build the
        /// control based on the configuration
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void CreateChildControls()
        {
            //First clear the controls collection
            Controls.Clear();

            //Create Table
            fileTable = new HtmlTable();

            AddFolderRow();
            AddFileRow();

            AddCommandRow();
            AddMessageRow();

            //Add table to Control
            this.Controls.Add(fileTable);

            //Call base class's method
            base.CreateChildControls();

        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// OnPreRender runs just before the control is rendered
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (cboFolders.Items.Count > 0)
            {
                //Configure Labels
                lblFolder.Text = KDLocalization.LocalizeString(this, "Folder");
                lblFolder.CssClass = LabelCssClass;
                lblFile.Text = KDLocalization.LocalizeString(this, "File");
                lblFile.CssClass = LabelCssClass;

                //select folder
                string fileName;
                string folderPath;
                if (!string.IsNullOrEmpty(FilePath))
                {
                    fileName = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
                    if (string.IsNullOrEmpty(fileName))
                        folderPath = FilePath;
                    else
                        folderPath = FilePath.Replace(fileName, "");
                }
                else
                {
                    fileName = FilePath;
                    folderPath = String.Empty;
                }

                if (cboFolders.Items.FindByValue(folderPath) != null)
                {
                    cboFolders.SelectedIndex = -1;
                    cboFolders.Items.FindByValue(folderPath).Selected = true;
                }
                cboFolders.Width = Width;

                //Get Files
                cboFiles.DataSource = GetFileList(!Required, cboFolders.SelectedItem.Value);
                cboFiles.DataBind();
                if (cboFiles.Items.FindByText(fileName) != null)
                    cboFiles.Items.FindByText(fileName).Selected = true;
                if (cboFiles.SelectedItem == null || String.IsNullOrEmpty(cboFiles.SelectedItem.Value))
                    FileID = -1;
                else
                    FileID = Int32.Parse(cboFiles.SelectedItem.Value);
                cboFiles.Width = Width;

                //Set up command buttons
                if (!String.IsNullOrEmpty(folderPath))
                    folderPath = folderPath.Substring(0, folderPath.Length - 1);

                string writeRoles = GetWriteRoles(folderPath);

                //Configure Mode
                switch (Mode)
                {
                    case FileControlMode.Normal:
                        fileRow.Visible = true;
                        folderRow.Visible = true;
                        cboFiles.Visible = true;
                        ShowImage();
                        txtFile.Visible = false;
                        if (PortalSecurity.IsInRoles(writeRoles) && ShowUpLoad)
                            ShowButton(cmdUpload, "UpLoad");
                        break;
                    case FileControlMode.UpLoadFile:
                        cboFiles.Visible = false;
                        txtFile.Visible = true;
                        imgPreview.Visible = false;
                        ShowButton(cmdSave, "Save");
                        ShowButton(cmdCancel, "Cancel");
                        break;
                }
            }
            else
                lblMessage.Text = KDLocalization.LocalizeString(this, "NoPermission");

            //Show message Row
            messageRow.Visible = (!String.IsNullOrEmpty(lblMessage.Text));
        }

        #endregion

        #region Event Handlers

        private void CancelUpload(object sender, EventArgs e)
        {
            Mode = FileControlMode.Normal;
        }

        private void FileChanged(object sender, EventArgs e)
        {
            SetFilePath();
        }

        private void FolderChanged(object sender, EventArgs e)
        {
            SetFilePath();
        }

        private void SaveFile(object sender, EventArgs e)
        {
            //if file is selected exit
            if (!String.IsNullOrEmpty(txtFile.PostedFile.FileName))
            {
                string folder = ParentFolder + cboFolders.SelectedItem.Value;

                string extension = System.IO.Path.GetExtension(txtFile.PostedFile.FileName).Replace(".", "");

                if (!String.IsNullOrEmpty(FileFilter) && !FileFilter.ToLower().Contains(extension.ToLower()))
                    // trying to upload a file not allowed for current filter
                    lblMessage.Text = String.Format(KDLocalization.LocalizeString(this, "UploadError"), FileFilter, extension);
                else
                    lblMessage.Text = FileSystemUtils.UploadFile(ParentFolder.Replace("/", @"\"), txtFile.PostedFile, false);

                if (String.IsNullOrEmpty(lblMessage.Text))
                {
                    string fileName = txtFile.PostedFile.FileName.Substring(txtFile.PostedFile.FileName.LastIndexOf(@"\") + 1);
                    SetFilePath(fileName);
                }
            }
            Mode = FileControlMode.Normal;
        }

        private void UploadFile(object sender, EventArgs e)
        {
            Mode = FileControlMode.UpLoadFile;
        }

        #endregion

    }
}
